package com.btpj.module_main.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.btpj.jetpack.arouter.treedetail.TreeDetailServiceImpWrap
import com.btpj.module_main.R
import com.btpj.module_main.data.entity.TreeData
import com.btpj.module_main.databinding.ListItemTreeBinding
import com.btpj.module_main.ui.widgets.tag_flow.TagFlowAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * 体系列表的Adapter
 *
 * @author LTP 2020/7/13
 */
class TreeListAdapter(layoutResId: Int = R.layout.list_item_tree) :
    BaseQuickAdapter<TreeData, BaseDataBindingHolder<ListItemTreeBinding>>(layoutResId) {

    override fun convert(holder: BaseDataBindingHolder<ListItemTreeBinding>, item: TreeData) {
        val binding = holder.dataBinding
        if (binding != null) {
            binding.treeData = item
            val treeDataItems = item.children
            binding.tagFlowLayout.setAdapter(object : TagFlowAdapter() {
                override fun getItemCount(): Int {
                    return treeDataItems.size
                }

                override fun createView(
                    inflater: LayoutInflater,
                    parent: ViewGroup,
                    position: Int
                ): View {
                    return LayoutInflater.from(context).inflate(R.layout.layout_tag, parent, false)
                }

                override fun bindView(view: View, position: Int) {
                    (view as TextView).text = treeDataItems[position].name
                }

                override fun onItemViewClick(view: View, position: Int) {
                    super.onItemViewClick(view, position)
                    val treeDataItem = treeDataItems[position]
                    TreeDetailServiceImpWrap.instance.start(
                        context,
                        treeDataItem.id,
                        treeDataItem.name
                    )
                }
            })

            binding.executePendingBindings()
        }
    }
}